﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IRepository.LY;
using DFGK.IService.LY;
using DFGK_WMS.Model.Models.LyModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service.LY
{
    public class QuoteService : BaseService, IQuoteService
    {
        private readonly IQuoteRepository _quoteRepository;
        private readonly IUnitOfWork _unitOfWork;
        public QuoteService(IMapper mapper, IQuoteRepository quoteRepository, IUnitOfWork unitOfWork) : base(mapper)
        {
            _quoteRepository = quoteRepository;
            _unitOfWork = unitOfWork;
        }
        public List<Quote> GetQuoteData(string? name, out int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            var list = _quoteRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(d => d.ProductName.Contains( name));
            }
            totalCount = list.Count();
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(d => d.Id);
            return list.ToList();
        }
        public bool AddOrModefiyQuote(Quote model, out string errorMsg)
        {
            errorMsg = string.Empty;
            if (model.Id > 0)
            {
                _quoteRepository.UpdEntity(model);
            }
            else
            {
                _quoteRepository.AddEntity(model);
              
            }
            return _unitOfWork.SaveChangesEntity();

        }

        public bool DelQuote(long quoteId)
        {
            var Quote = _quoteRepository.QueryEntityById(quoteId);
            if (Quote == null)
            {
                return false;
            }
            _quoteRepository.DelEntity(Quote);
            return _unitOfWork.SaveChangesEntity();
        }

        public Quote? GetQuote(long quoteId)
        {
            return _quoteRepository.QueryEntityById(quoteId);
        }

       
    }
}
